package com.df.my_net_game.common.config;

import com.df.my_net_game.game_rpc.EnableEZGame;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class OnProfileCondition extends SpringBootCondition {
    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String[] beanNamesForAnnotation = context.getBeanFactory().getBeanNamesForAnnotation(EnableEZGame.class);
        if(beanNamesForAnnotation!=null && beanNamesForAnnotation.length>0){
            String s = beanNamesForAnnotation[0];
            Object bean = context.getBeanFactory().getBean(s);
            EnableEZGame annotation = bean.getClass().getAnnotation(EnableEZGame.class);
            String[] strings = annotation.docEnv();
            if(strings==null || strings.length==0){
                return new ConditionOutcome(false,"未配置游戏doc的适用环境，不开启游戏doc服务");
            }
            if(context.getEnvironment().matchesProfiles(strings)){
                return new ConditionOutcome(true,"开启游戏doc服务");
            }
            return new ConditionOutcome(false,"当前环境不开启游戏doc服务");
        }
        return new ConditionOutcome(false,"");

    }
}
